package com.base.kit;

import android.text.TextUtils;

import androidx.annotation.IntRange;
import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {
    public static final String TIME_ALL = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_WEEK = "yyyy-MM-dd EEEE";
    public static final String TIME_YMD = "yyyy-MM-dd";
    public static final String TIME_HM = "HH:mm";
    public static final String TIME_HMS = "HH:mm:ss";
    public static final String TIME_MDHM = "MM-dd HH:mm";
    public static final String TIME_YM = "yyyy-MM";

    public static SimpleDateFormat setFormat(String format) {
        return new SimpleDateFormat(format, Locale.CHINA);
    }

    public static String getWeek() {
        return get(new Date(), TIME_WEEK);
    }

    public static String get(Date date, String format) {
        return replaceTime(setFormat(format).format(date));
    }

    /**
     * @return 2021-01-01
     */
    public static String getDate() {
        return getDate(TIME_YMD);
    }

    public static String getDate(String format) {
        return getDate(new Date(), format);
    }

    public static String getDate(Date date, String format) {
        return get(date, format);
    }

    /**
     * @return 2021-01-01 11:22:33
     */
    public static String getDateTime() {
        return getDateTime(TIME_ALL);
    }

    public static String getDateTime(String format) {
        return getDateTime(new Date(), format);
    }

    public static String getDateTime(Date date, String format) {
        return get(date, format);
    }

    /**
     * @return 11:22:33
     */
    public static String getTime() {
        return getTime(TIME_HMS);
    }

    public static String getTime(String format) {
        return getTime(new Date(), format);
    }

    public static String getTime(Date date, String format) {
        return get(date, format);
    }

    public static boolean afterDate(String startTime, String endTime, String format) {
        try {
            SimpleDateFormat sdf = setFormat(format);
            if (TextUtils.isEmpty(startTime) || TextUtils.isEmpty(endTime))
                return true;
            if (TextUtils.equals(startTime, endTime))
                return true;
            Date start = sdf.parse(startTime);
            Date end = sdf.parse(endTime);
            return end.after(start);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @param startTime 开始时间
     * @param endTime   结束时间
     *                  如：2018-07-01,2018-07-02,相差1天
     * @return 两个时间段相隔几天
     */
    public static float daysDifference(String startTime, String endTime) {
        return daysDifference(startTime, endTime, TIME_YMD);
    }

    public static float daysDifference(String startTime, String endTime, String format) {
        return daysDifference(startTime, endTime, format, 24);
    }

    /**
     * @param hour 12小时制还是24小时制
     */
    public static float daysDifference(String startTime, String endTime, String format, @IntRange(from = 1, to = 24) int hour) {
        return daysDifference(startTime, endTime, format, hour, false);
    }

    /**
     * @param hour         12小时制还是24小时制
     * @param addStartTime 是否算上起始时间
     */
    public static float daysDifference(String startTime, String endTime, String format, @IntRange(from = 1, to = 24) int hour, boolean addStartTime) {
        try {
            SimpleDateFormat sdf = setFormat(format);
            if (TextUtils.isEmpty(startTime) || TextUtils.isEmpty(endTime))
                return -1f;
            if (TextUtils.equals(startTime, endTime))
                return 0f;
            Date start = sdf.parse(replaceTime(startTime));
            Date end = sdf.parse(replaceTime(endTime));
            long span = (end.getTime() - start.getTime()) / 1000L;
            float day = (float) span / (hour * 3600);
            return day >= 0 ? addStartTime ? day + 1 : day : -1f;
            //DecimalFormat df = new DecimalFormat("#.00");
            //return df.format(days);
        } catch (Exception e) {
            e.printStackTrace();
            return -1f;
        }
    }

    /**
     * format时间格式转换为long
     *
     * @param time 默认格式为yyyy-MM-dd HH:mm:ss
     */
    public static long time2LongDate(String time) {
        return time2LongDate(time, TIME_ALL);
    }

    /**
     * @param time   必须和所传的时间格式相匹配
     * @param format 默认yyyy-MM-dd HH:mm:ss
     */
    public static long time2LongDate(String time, String format) {
        try {
            if (TextUtils.isEmpty(time))
                return 0L;
            Date start = setFormat(format).parse(time);
            return start.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    public static String replaceTime(String str) {
        return str.toLowerCase().replace("上午", "").replace("下午", "").replace("am", "").replace("pm", "");
    }

    public static String subEndTime(@Nullable CharSequence timeSequence) {
        String subTime = ResUtil.replaceText(timeSequence).trim();
        return TextUtils.isEmpty(subTime) ? "" : subTime.
                replace("00:00:00", "").replace(".0", "").trim();
    }
}
